package com.tedu.udm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tedu.udm.ex.ServiceException;
import com.tedu.udm.mapper.ClassMapper;
import com.tedu.udm.mapper.ClassTeamMapper;
import com.tedu.udm.mapper.UserMapper;
import com.tedu.udm.pojo.dto.ClassAddNewDTO;
import com.tedu.udm.pojo.dto.ClassUpdateDTO;
import com.tedu.udm.pojo.entity.Classes;
import com.tedu.udm.pojo.vo.ClassListItemVO;
import com.tedu.udm.pojo.vo.ClassStandardVO;
import com.tedu.udm.service.IClassService;
import com.tedu.udm.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理班级数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Service
public class ClassServiceImpl implements IClassService {
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private ClassTeamMapper classTeamMapper;
    @Autowired
    private UserMapper userMapper;

    public ClassServiceImpl() {
        log.debug("创建业务对象：ClassServiceImpl");
    }

    @Override
    public void addNew(ClassAddNewDTO classAddNewDTO) {
        log.debug("开始处理【添加班级】的业务，参数：{}", classAddNewDTO);

        {
            String className = classAddNewDTO.getClassName();
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("class_name",className);
            Classes classes = classMapper.selectOne(wrapper);
            if (classes != null) {
                String message = "添加班级失败，班级名【" + className + "】已经被占用！";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        {
            Long teacherId = classAddNewDTO.getTeacherId();
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("id",teacherId);
            wrapper.last("AND is_teacher=1");
            int count = userMapper.selectCount(wrapper);
            if (count < 1) {
                String message = "添加班级失败，班级老师信息有误！";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        Classes classes = new Classes();
        // 通过BeanUtils.copyProperties()方法将参数对象的各属性值复制到Admin对象中
        BeanUtils.copyProperties(classAddNewDTO, classes);
        int rows = classMapper.insert(classes);
        if (rows != 1) {
            String message = "添加班级失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除班级】的业务，参数：{}",id);
        {
            log.debug("检查是否存在与【删除班级】关联的小组数据！");
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("class_id",id);
            int count = classTeamMapper.selectCount(wrapper);
            if (count > 0) {
                String message = "删除班级失败，本班级存在关联的小组信息！";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        {
            log.debug("检查是否存在与【删除班级】关联的用户数据！");
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("class_id",id);
            int count = userMapper.selectCount(wrapper);
            if (count > 0) {
                String message = "删除班级失败，本班级存在关联的用户信息！";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        log.debug("检查通过，准备【删除班级】数据！");
        int rows = classMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除班级失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void update(ClassUpdateDTO classUpdateDTO) {
        log.debug("开始处理【修改班级】的业务，参数：{}",classUpdateDTO);

        {
            String className = classUpdateDTO.getClassName();
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("class_name",className);
            Classes classes = classMapper.selectOne(wrapper);
            if (classes != null) {
                String message = "修改班级失败，班级名【" + className + "】已经被占用！";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        {
            Long teacherId = classUpdateDTO.getTeacherId();
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("id",teacherId);
            wrapper.last("AND is_teacher=1");
            int count = userMapper.selectCount(wrapper);
            if (count < 1) {
                String message = "修改班级失败，班级老师信息有误！";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        Classes classes = new Classes();
        // 通过BeanUtils.copyProperties()方法将参数对象的各属性值复制到Admin对象中
        BeanUtils.copyProperties(classUpdateDTO, classes);
        int rows = classMapper.updateById(classes);
        if (rows != 1) {
            String message = "修改班级失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public ClassStandardVO get(Long id) {
        log.debug("开始处理【班级详情】的业务，无参数");
        Classes classes = classMapper.selectById(id);
        ClassStandardVO classStandardVO = new ClassStandardVO();
        BeanUtils.copyProperties(classes,classStandardVO);
        return classStandardVO;
    }

    @Override
    public List<ClassListItemVO> list() {
        log.debug("开始处理【班级列表】的业务，无参数");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("create_date");
        List<Classes> lists = classMapper.selectList(wrapper);
        List<ClassListItemVO> classListItemVOList = new ArrayList<>() ;
        for (Classes l:lists) {
            ClassListItemVO classListItemVO = new ClassListItemVO();
            BeanUtils.copyProperties(l,classListItemVO);
            classListItemVOList.add(classListItemVO);
        }
        return classListItemVOList;
    }

    @Override
    public List<ClassListItemVO> myClassList(Long id) {
        log.debug("开始处理【所属班级列表】的业务，无参数");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("teacher_id",id);
        wrapper.orderByDesc("create_date");
        List<Classes> lists = classMapper.selectList(wrapper);
        List<ClassListItemVO> classListItemVOList = new ArrayList<>() ;
        for (Classes l:lists) {
            ClassListItemVO classListItemVO = new ClassListItemVO();
            BeanUtils.copyProperties(l,classListItemVO);
            classListItemVOList.add(classListItemVO);
        }
        return classListItemVOList;
    }
}
